package com.gtworldmobile.json;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONManager {

	public static JSONObject getJSONfromURL(String url,String grupo) {
		InputStream is = null;
		String result = "";
		JSONObject json = null;
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet(url);
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
			is.close();
			result = sb.toString();
			if(!grupo.equals("poi")){				
				String format = "{ @" + grupo + "@:";
				format = format.replace('@', '"');			
				result = format + result + " }";			
			}else{
				String format = "{ @" + grupo + "@:[";
				format = format.replace('@', '"');			
				result = format + result + " ]}";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			json = new JSONObject(result);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return json;
	}
}